package com.jrfid.smartcabinet.demo.ui.take_or_add

import android.content.DialogInterface
import android.graphics.Color
import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.Observer
import androidx.navigation.fragment.navArgs
import com.jrfid.smartcabinet.demo.R
import com.jrfid.smartcabinet.demo.base.BaseFragment
import com.jrfid.smartcabinet.demo.databinding.TakeOrAddGoodsFragmentBinding
import com.jrfid.smartcabinet.demo.ui.setting.SettingFragmentArgs

class TakeOrAddGoodsFragment : BaseFragment<TakeOrAddGoodsFragmentBinding, TakeOrAddGoodsViewModel>(TakeOrAddGoodsViewModel::class.java) {

    companion object {
        fun newInstance() = TakeOrAddGoodsFragment()
    }

    override fun getLayoutId(): Int = R.layout.take_or_add_goods_fragment

    private val args by navArgs<TakeOrAddGoodsFragmentArgs>()

    override fun setData() {
        getViewModel()?.isTake?.set(args.isTake)
        getDataBinding()?.lottieChecking?.setAnimation(if (args.isTake) R.raw.checking_03dac5 else R.raw.checking_018786)
        getViewModel()?.startAnimation?.observe(this, Observer {
            if (it) {
                getDataBinding()?.lottieChecking?.playAnimation()
            } else {
                getDataBinding()?.lottieChecking?.cancelAnimation()
            }
        })
        getViewModel()?.showTipsDialog?.observe(this, Observer {
            if (!it.isNullOrEmpty()) {
                getViewModel()?.showTipsDialog?.value = ""
                AlertDialog.Builder(requireContext())
                    .setTitle(R.string.text_dialog_title_tips)
                    .setMessage(it)
                    .setPositiveButton(R.string.btn_confirm) { d, w ->
                        getViewModel()?.back()
                    }
                    .show()
            }
        })
    }

    override fun onResume() {
        super.onResume()
        getDataBinding()?.lottieChecking?.resumeAnimation()
    }

    override fun onPause() {
        super.onPause()
        getDataBinding()?.lottieChecking?.pauseAnimation()
    }

}